package com.yequan.leeetcoden;

/**
 * @author yequan
 * @date 2021/06/14 16:37
 **/
public class MoveZero {

    public static void main(String[] args) {

    }

    public static void moveZero(int[] nums) {
        if (nums == null || nums.length < 2) {
            return;
        }
        int nextNonZeroIndex = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                nums[nextNonZeroIndex] = nums[i];
                if (nextNonZeroIndex != i) {
                    nums[i] = 0;
                }
                nextNonZeroIndex++;
            }
        }
    }

    public static void moveZero2(int[] nums) {
        if (nums == null || nums.length < 2) {
            return;
        }
        int nextNonZeroIndex = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                nums[nextNonZeroIndex] = nums[i];
                if (nextNonZeroIndex != i) {
                    nums[i] = 0;
                }
                nextNonZeroIndex++;
            }
        }
    }

}